<?php

class IndexController extends Zend_Controller_Action
{
    public function testAction()
    {
		$this->_helper->ViewRenderer->setNoRender(true);
		$db = $this->_helper->ConnManager->getConnection();


    	Zend_Db_Table::setDefaultAdapter($db);

    	$functions = new Functions;
    	
    	//var_dump($functions->info());
    	
		$result1 = $functions->find(42, '///00/adm.h.', 'db2db', 2);
		
		$select = $functions->select();
		$select->where('id = ?', 42);
		$result = $functions->fetchAll();
		
		var_dump($result1->toArray());
		var_dump($result->toArray());


    }
    
    public function test2Action() {
    	$this->_helper->ViewRenderer->setNoRender(true);
    	$adapter = new Auth_Adapter_Db('login','pswd');
		$result = $adapter->authenticate();

    }
    
    public function fioAction()
    {
    	$persons = new Persons;
    	
    	$person_id = $this->_request->getParam('person_id');
    	$person = $persons->find($person_id)->current();
    	die($person->getTitle());
    }

	public function indexAction()
	{
	}
    
    
    public function gridAction()
    {

	$export = $this->_request->getParam('export');
    switch ($export)
        {
            case 'odt' :
                $grid = "Bvb_Grid_Deploy_Odt";
                break;
            case 'ods' :
                $grid = "Bvb_Grid_Deploy_Ods";
                break;
            case 'xml' :
                $grid = "Bvb_Grid_Deploy_Xml";
                break;
            case 'csv' :
                $grid = "Bvb_Grid_Deploy_Csv";
                break;
            case 'excel' :
                $grid = "Bvb_Grid_Deploy_Excel";
                break;
            case 'word' :
                $grid = "Bvb_Grid_Deploy_Word";
                break;
            case 'wordx' :
                $grid = "Bvb_Grid_Deploy_Wordx";
                break;
            case 'pdf' :
                $grid = "Bvb_Grid_Deploy_Pdf";
                break;
            case 'print' :
                $grid = "Bvb_Grid_Deploy_Print";
                break;
            default :
                $grid = "Bvb_Grid_Deploy_Table";
                break;
        }
     	$this->_helper->layout->disableLayout();	
    	$db = $this->_helper->ConnManager->getConnection();
    	$db->setFetchMode(Zend_Db::FETCH_ASSOC);
    	Zend_Db_Table::setDefaultAdapter($db);
    	$grid = new $grid($db,'Document title','/tmp', array('download'));
		$grid->escapeOutput ( false );
		$grid->addTemplateDir('Kpl/Grid/Template/Table', 'Kpl_Grid_Template_Table', 'table');
		$grid->setTemplate('iconv','table');
		$grid->from('street');
		$grid->imagesUrl = $this->view->baseUrl . '/application/public/grid/images/';
    	$this->view->grid = $grid->deploy();
    	
    }
    
    public function dumpAction()
    {
    	$fc = Zend_Controller_Front::getInstance();
    	$fc->unregisterPlugin('Zend_Controller_Plugin_ActionStack');
    	$this->_helper->layout->disableLayout();
    	$this->_helper->ViewRenderer->setNoRender(true);
    	$model = $this->_request->getParam('model')
    			? $this->_request->getParam('model') 
    			: 'functions' ;
    	
    	$db = $this->_helper->ConnManager->getConnection();
    	$db->setFetchMode(Zend_Db::FETCH_ASSOC);
    	Zend_Db_Table::setDefaultAdapter($db);
    	
    	$m = new $model; 
    	
    	$select = $m->select();;
    	$select->limit(100);
    	$result = $m->fetchAll($select);
    	
    	$info = $m->info();
    	
    	$tableName = $info['name'];
    	$cols = implode("','",$info['cols']);
    	
    	
    	
    	foreach($result as $row)
    	{
    		$values_array = array();
    		foreach($info['cols'] as $field)
    		{
    			$value = ($field == 'id_bit') ? 'YYY':$row[$field]; 
    			$values_array[] = $value;
    		}
    		$values = implode("','", $values_array);
    		$sqls[] = "insert into {$tableName} ('{$cols}') values ('{$values}')";
    	}
    	$inserts = implode("\"\n,\"",$sqls);
    	
    	echo Util::iconv("\${$model}_dump = array(\n\"{$inserts}\")");
    	//if($)
    	//$result = $db->query('select * from ' . $table);
    	
    }

	public function cascadeAction()
	{
		$config = array();
		$config[_meta] = array('select1' => array(
								'select2' => 'select4' 
							   ,'select3'));
		$config[elements][select1] = array('ajax'=> array('url' => '111'));
		$config[elements][select2] = array('ajax'=> array('url' => '222'));
		$config[elements][select3] = array('ajax'=> array('url' => '333'));
		$config[elements][select4] = array('ajax'=> array('url' => '444'));

		$form = new ZendX_JQuery_Form;
		$cascade = new ZendX_JQuery_Form_Element_CascadeSelect('cascade',array('jQueryParams'=>$config));
		$form->addElement($cascade);
		$this->view->form = $form;
	}

	public function init()
	{
		Zend_Db_Table::setDefaultAdapter($this->_helper->ConnManager->getConnection());
		$this->view->addHelperPath(dirname(__FILE__) . '/../views/helpers', 'Kpl_View_Helper');
	}


}
